package zym.diandian;

import android.app.ActivityManager;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import zym.diandian.module.popup.PopupFrame;
import zym.diandian.module.sys.BatteryBroadcastRec;
import zym.diandian.module.sys.InfoFrame;
import zym.diandian.module.volume.VolumeFrame;
import zym.diandian.module.wifi.WifiFrame;
import zym.diandian.module.wireless.WirelessFrame;
import zym.diandian.module.zan.ZanFrame;

public class MainActivity extends AppCompatActivity {

    private FragmentTransaction fragmentTransaction;
    private DrawerLayout drawerLayout;
    private NavigationView navigationView;

    private MenuItem lastClickItem;

    private static Fragment main_fragment, volume_fragment, pop_fragment,
            wireless_fragment, info_fragment,zan_fragment,wifi_fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);


        if (getSupportActionBar() != null) {
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        } else {
            Toast.makeText(getApplicationContext(), "getSupportActionBar null", Toast.LENGTH_SHORT).show();
        }

        //
        this.fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.content_layout, new Main_Content()).commit();

        currentFragment = new Main_Content();

        //
        this.drawerLayout = (DrawerLayout) this.findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close);
        drawerToggle.syncState();
        drawerLayout.addDrawerListener(drawerToggle);
        this.navigationView = (NavigationView) this.findViewById(R.id.navigation_layout);

        //
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {

                lastClickItem = item;

                fragmentTransaction = getSupportFragmentManager().beginTransaction();
                switch (item.getItemId()) {
                    case R.id.navigation_wireless:
                        if (wireless_fragment == null) {
                            wireless_fragment = new WirelessFrame();
                        }
                        switchContent(currentFragment, wireless_fragment);
                        //fragmentTransaction.replace(R.id.content_layout, new WirelessFrame()).commit();
                        break;
                    case R.id.navigation_home:
                        if (main_fragment == null) {
                            main_fragment = new Main_Content();
                        }
                        switchContent(currentFragment, main_fragment);
                        //fragmentTransaction.replace(R.id.content_layout, new Main_Content()).commit();
                        break;
                    case R.id.navigation_vol:
                        if (volume_fragment == null) {
                            volume_fragment = new VolumeFrame();
                        }
                        switchContent(currentFragment, volume_fragment);
                        //fragmentTransaction.replace(R.id.content_layout, new VolumeFrame()).commit();
                        break;
                    case R.id.navigation_pop:
                        //fragmentTransaction.add(new PopupFrame(), "pop");
                        if (pop_fragment == null) {
                            pop_fragment = new PopupFrame();
                        }
                        switchContent(currentFragment, pop_fragment);
                        //fragmentTransaction.replace(R.id.content_layout, new PopupFrame()).commit();
                        break;
                    case R.id.navigation_sysinfo:
                        if (info_fragment == null) {
                            info_fragment = new InfoFrame();
                        }
                        switchContent(currentFragment, info_fragment);
                        break;
                    case R.id.navigation_zan:
                        if(zan_fragment ==null){
                            zan_fragment = new ZanFrame();
                        }
                        switchContent(currentFragment,zan_fragment);
                        break;
                    case R.id.navigation_wifi:
                        if(wifi_fragment == null){
                            wifi_fragment = new WifiFrame();
                        }
                        switchContent(currentFragment,wifi_fragment);
                        break;
                    default:
                        break;
                }
                //close drawer layout
                drawerLayout.closeDrawers();

                setTitle(item.getTitle());
                return false;
            }
        });

        {
            //初始化
            IntentFilter batteryFilte = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
            registerReceiver(new BatteryBroadcastRec(), batteryFilte);
        }

    }

    //当前正在显示的Fragment
    private Fragment currentFragment = null;

    public void switchContent(Fragment from, Fragment to) {
        if (from != to) {
            if (!to.isAdded()) {    // 先判断是否被add过
                fragmentTransaction.hide(from).add(R.id.content_layout, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                fragmentTransaction.hide(from).show(to).commit(); // 隐藏当前的fragment，显示下一个
            }

            currentFragment = to;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        System.out.println("option item id " + item.getItemId());
        switch (item.getItemId()) {
            case android.R.id.home:
                if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                    drawerLayout.closeDrawer(GravityCompat.START);
                } else {
                    drawerLayout.openDrawer(GravityCompat.START);
                }
                return true;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private long lastBackTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_BACK:

                if (System.currentTimeMillis() - lastBackTime > 1500) {
                    Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                    lastBackTime = System.currentTimeMillis();
                    return true;
                } else {
                    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
                    manager.killBackgroundProcesses(getPackageName());
                    return false;
                }
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
}
